package pl.PawelAndAlicja.badugiproject.main.Cards;

import java.util.ArrayList;
import java.util.Collections;

class FlyweightFactoryCard{

    public FlyweightFactoryCard() {
        this.flyweightSpadeCards = new ArrayList<FlyweightSpadeCard>();
        this.flyweightClubCards = new ArrayList<FlyweightClubCard>();
        this.flyweightDiamondCards = new ArrayList<FlyweightDiamondCard>();
        this.flyweightHeartCards = new ArrayList<FlyweightHeartCard>();
    }

    public FlyweightSpadeCard getCard(FlyweightSpadeCard card) {
        boolean isExist = false;
        int index;
        for (index = 0; index < flyweightSpadeCards.size(); index++)
            if (flyweightSpadeCards.get(index).getName().equals(card.getName())) {
                isExist = true;
                break;
            }
        if (isExist)
            return flyweightSpadeCards.get(index);
        else {
            FlyweightSpadeCard flyweightCard = new FlyweightSpadeCard(card.getName());
            flyweightSpadeCards.add(flyweightCard);
            return flyweightCard;
        }
    }

    public FlyweightClubCard getCard(FlyweightClubCard card) {
        boolean isExist = false;
        int index;
        for (index = 0; index < flyweightClubCards.size(); index++)
            if (flyweightClubCards.get(index).getName().equals(card.getName())) {
                isExist = true;
                break;
            }
        if(isExist)
            return flyweightClubCards.get(index);
        else {
            FlyweightClubCard flyweightCard = new FlyweightClubCard(card.getName());
            flyweightClubCards.add(flyweightCard);
            return flyweightCard;
        }
    }

    public FlyweightDiamondCard getCard(FlyweightDiamondCard card) {
        boolean isExist = false;
        int index;
        for (index = 0; index < flyweightDiamondCards.size(); index++)
            if (flyweightDiamondCards.get(index).getName().equals(card.getName())) {
                isExist = true;
                break;
            }
        if(isExist)
            return flyweightDiamondCards.get(index);
        else {
            FlyweightDiamondCard flyweightCard = new FlyweightDiamondCard(card.getName());
            flyweightDiamondCards.add(flyweightCard);
            return flyweightCard;
        }
    }

    public FlyweightHeartCard getCard(FlyweightHeartCard card) {
        boolean isExist = false;
        int index;
        for (index = 0; index < flyweightHeartCards.size(); index++)
            if (flyweightHeartCards.get(index).getName().equals(card.getName())) {
                isExist = true;
                break;
            }
        if(isExist)
            return flyweightHeartCards.get(index);
        else {
            FlyweightHeartCard flyweightCard = new FlyweightHeartCard(card.getName());
            flyweightHeartCards.add(flyweightCard);
            return flyweightCard;
        }
    }

    public void removeLastCard(String typeCard) {
        if (typeCard.equals("Spade")) {
            flyweightSpadeCards.remove(flyweightSpadeCards.size() - 1);
        } else if (typeCard.equals("Club")) {
            flyweightClubCards.remove(flyweightClubCards.size() - 1);
        } else if (typeCard.equals("Diamond")) {
            flyweightDiamondCards.remove(flyweightDiamondCards.size() - 1);
        } else if (typeCard.equals("Heart")) {
            flyweightHeartCards.remove(flyweightHeartCards.size() - 1);
        }
    }

    public void removeCard(String typeCard, String nameCard) {
        if (typeCard.equals("Spade")) {
            flyweightSpadeCards.remove(getIndexCard(typeCard, nameCard));
        } else if (typeCard.equals("Club")) {
            flyweightClubCards.remove(getIndexCard(typeCard, nameCard));
        } else if (typeCard.equals("Diamond")) {
            flyweightDiamondCards.remove(getIndexCard(typeCard, nameCard));
        } else if (typeCard.equals("Heart")) {
            flyweightHeartCards.remove(getIndexCard(typeCard, nameCard));
        }
    }

    public void showArrayCards(){
        System.out.print("Spade = ");
        showArrayDeckType("Spade");
        System.out.println();
        System.out.print("Club = ");
        showArrayDeckType("Club");
        System.out.println();
        System.out.print("Diamond = ");
        showArrayDeckType("Diamond");
        System.out.println();
        System.out.print("Heart = ");
        showArrayDeckType("Heart");
        System.out.println();
    }

    void showArrayDeckType(String typeCard) {
        if (typeCard.equals("Spade")) {
            for(FlyweightSpadeCard elem : flyweightSpadeCards) {
                System.out.print(elem+ ", ");
            }
        } else if (typeCard.equals("Club")) {
            for(FlyweightClubCard elem : flyweightClubCards) {
                System.out.print(elem + ", ");
            }
        } else if (typeCard.equals("Diamond")) {
            for(FlyweightDiamondCard elem : flyweightDiamondCards) {
                System.out.print(elem + ", ");
            }
        } else if (typeCard.equals("Heart")) {
            for(FlyweightHeartCard elem : flyweightHeartCards) {
                System.out.print(elem + ", ");
            }
        }
    }

    public void shuffleDeck(String typeCard) {
        if (typeCard.equals("Spade")) {
            Collections.shuffle(flyweightSpadeCards);
        } else if (typeCard.equals("Club")) {
            Collections.shuffle(flyweightClubCards);
        } else if (typeCard.equals("Diamond")) {
            Collections.shuffle(flyweightDiamondCards);
        } else if (typeCard.equals("Heart")) {
            Collections.shuffle(flyweightHeartCards);
        }
    }

    public int sizeCardsFactory() {
        return flyweightClubCards.size() + flyweightSpadeCards.size() + flyweightDiamondCards.size() + flyweightHeartCards.size();
    }


    private int getIndexCard(String typeCard, String cardName) {
        int i = 0;
        if (typeCard.equals("Spade")) {
            for ( i = 0; i < flyweightSpadeCards.size(); i++) {
                if (flyweightSpadeCards.get(i).getName().equals(cardName))
                    break;
            }
        } else if (typeCard.equals("Club")) {
            for ( i = 0; i < flyweightClubCards.size(); i++) {
                if(flyweightClubCards.get(i).getName().equals(cardName))
                    break;
            }
        } else if (typeCard.equals("Diamond")) {
            for ( i = 0; i < flyweightDiamondCards.size(); i++) {
                if(flyweightDiamondCards.get(i).getName().equals(cardName))
                    break;
            }
        } else if (typeCard.equals("Heart")) {
            for ( i = 0; i < flyweightHeartCards.size(); i++) {
                if(flyweightHeartCards.get(i).getName().equals(cardName))
                    break;
            }
        }

        /*
         * TODO
        *  Proba latania bugu gdzie odwolujemy sie do INDEX: 12, Size: 12
        */
        if ( (i == flyweightSpadeCards.size()) || (i == flyweightSpadeCards.size()) ||
                (i == flyweightSpadeCards.size()) ||(i == flyweightSpadeCards.size()) )
            i--;


        return i;
    }


    private final ArrayList<FlyweightSpadeCard> flyweightSpadeCards;
    private final ArrayList<FlyweightClubCard> flyweightClubCards;
    private final ArrayList<FlyweightDiamondCard> flyweightDiamondCards;
    private final ArrayList<FlyweightHeartCard> flyweightHeartCards;

}
